﻿angular.module('app')
    .service('UserSrv', ['$http','$q','helperSrv',
        function ($http, $q, helperSrv) {
            return {
                getUserByName: function (name,abortable) {
                    var defered = $q.defer();
                    $http({ method: 'GET', url: 'api/users', params: { name: name }, cache: false, headers: { 'Abortable': abortable } }).success(function (dt, st) {
                        defered.resolve(dt);
                    });
                    return defered.promise;
                    //return userRsrc.get({ name: name }).$promise;
                },
                authorize: function (username, password, remember) {
                    // PG. made this change
                    return $http({ method: 'POST', url: 'account/logon', data: { userName: username, password: password, rememberMe: remember, returnUrl: '' }, cache: false });
                    //return $http({ method: 'POST', url: 'api/users', data: { userName: username, password: password, rememberMe: remember, returnUrl: '' }, cache: false });
                    //return userRsrc.login({ userName: username, password: password, rememberMe: remember, returnUrl: '' }).$promise;
                },
                logout: function () {
                    return $http({ method: 'GET', url: 'api/users', cache: false });
                    //return userRsrc.login({ userName: username, password: password, rememberMe: remember, returnUrl: '' }).$promise;
                },
                updateUserInfo: function (userInfo) {
                    //Create new object
                    var userObject = {};
                    //Copy angular model to new object to prevent binding new data
                    angular.copy(userInfo, userObject);
                    //Check if user birthday not null
                    if (userObject.BirthdayStr !== '') {
                        //convert formated date string to date object
                        var birthDay = helperSrv.convertFormatedStringToDateObject(userObject.BirthdayStr);
                        //convert date object to server date
                        userObject.BirthdayStr = helperSrv.convertDateObjectToServerDate(birthDay);
                    }
                    return $http({ method: 'PATCH', url: 'api/users', data: userObject, cache: false });
                },
                updateUserPassword: function(userPassword) {
                    return $http({ method: 'POST', url: 'api/users/password/' + userPassword.name + '/' + userPassword.oldp + '/' + userPassword.newp, data: userPassword, cache: false });
                },
                removeFollowing: function (follower) {
                    return $http({ method: 'DELETE', url: 'api/users/follows/' + follower.userName + '/' + follower.followerName, cache: false });
                },
                followUser: function (follower) {
                    return $http({ method: 'POST', url: 'api/users/follows/' + follower.userName + '/' + follower.followerName, cache: false });
                },
                uploadAvatar: function (url,avatar) {
                    return $http({
                        method: 'POST', url: url, data: avatar, cache: false, headers: { 'Content-Type': undefined },
                        transformRequest: function (data) {
                            if (typeof FormData != 'undefined') {
                                var fd = new FormData();
                                angular.forEach(data, function (value, key) {
                                    fd.append(key, value);
                                });
                                return fd;
                            }
                        }
                    });
                },
                searchUser: function (nameToSearch) {
                    return $http({ method: 'GET', url: 'api/users',params:nameToSearch, cache: false });
                },
                //api/users/stats/cgrant
                getUserStats:function(user) {
                    return $http({ method: 'GET', url: 'api/users/stats/' + user, cache: false, headers: { 'Abortable': false } });
                }
            };
        }
    ]);